package org.Iteracion4.presentacion;

import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;

import java.awt.BorderLayout;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.WindowEvent;

import javax.swing.JFormattedTextField;
import javax.swing.text.MaskFormatter;






import org.Iteracion1.presentacion.Menu;
import org.Iteracion4.dominio.Usuario;

import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;
import java.awt.event.WindowAdapter;
import java.awt.Font;

public class Login {

	public JFrame VentanaLogin;
	private JPanel pnlLogin;
	private JLabel lblDni;
	private JLabel lblContrasea;
	private JPasswordField psswContra;
	private JButton btnIdentificarse;
	private JFormattedTextField txtDni;
	private JButton btnRegistrarse;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Login window = new Login();
					window.VentanaLogin.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 * @throws java.text.ParseException 
	 */
	public Login() throws java.text.ParseException {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 * @throws java.text.ParseException 
	 */
	private void initialize() throws java.text.ParseException {
		VentanaLogin = new JFrame();
		VentanaLogin.addWindowListener(new FrameWindowListener());
		VentanaLogin.setResizable(false);
		VentanaLogin.setTitle("Identif\u00EDcate");
		VentanaLogin.setBounds(100, 100, 247, 221);
		VentanaLogin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		{
			pnlLogin = new JPanel();
			VentanaLogin.getContentPane().add(pnlLogin, BorderLayout.CENTER);
			pnlLogin.setLayout(null);
			{
				lblDni = new JLabel("DNI:");
				lblDni.setBounds(70, 13, 56, 16);
				pnlLogin.add(lblDni);
			}
			{
				lblContrasea = new JLabel("Contrase\u00F1a");
				lblContrasea.setBounds(70, 67, 90, 16);
				pnlLogin.add(lblContrasea);
			}
			{
				psswContra = new JPasswordField();
				psswContra.setBounds(70, 91, 116, 22);
				psswContra.addFocusListener(new MiFocusListener());
				pnlLogin.add(psswContra);
			}
			{
				btnIdentificarse = new JButton("Identificarse");
				btnIdentificarse.setFont(new Font("Tahoma", Font.BOLD, 12));
				btnIdentificarse.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						try {
							Usuario u=new Usuario(txtDni.getText(),null,null,null,null);
							u.leerUsuario();
							if(u.getContrasena().equals(psswContra.getText())){
								Menu m = new Menu();
								m.VentanaMenu.setVisible(true);
								VentanaLogin.setVisible(false);	
							}else{
								JOptionPane.showMessageDialog(VentanaLogin,"Contraseña erronea.","Aviso",JOptionPane.ERROR_MESSAGE);
							}
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
							JOptionPane.showMessageDialog(VentanaLogin,"Usuario no encontrado.","Aviso",JOptionPane.ERROR_MESSAGE);
							
							
						}
					}
				});
				btnIdentificarse.setBounds(56, 124, 142, 25);
				pnlLogin.add(btnIdentificarse);
			}
			{
				MaskFormatter formatoDNI;
				try {
					formatoDNI = new MaskFormatter("########'-U");
					formatoDNI.setPlaceholderCharacter('X');
					txtDni = new JFormattedTextField(formatoDNI);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				txtDni.setBounds(70, 36, 90, 20);
				txtDni.addFocusListener(new MiFocusListener());
				pnlLogin.add(txtDni);
			}
			{
				btnRegistrarse = new JButton("Registrarse");
				btnRegistrarse.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						Nuevo_Usuario n=new Nuevo_Usuario();
						n.frmRegistrarNuevoUsuario.setVisible(true);
					}
				});
				btnRegistrarse.setBounds(70, 160, 116, 23);
				pnlLogin.add(btnRegistrarse);
			}
		}
	}
	private class MiFocusListener extends FocusAdapter {
		@Override
		public void focusGained(FocusEvent e) {
			e.getComponent().setBackground(new Color(250,250,210));
		}
		@Override
		public void focusLost(FocusEvent e) {
			e.getComponent().setBackground(new Color(250,250,250));
		}
	}
	
	private class FrameWindowListener extends WindowAdapter {
		@Override
		public void windowClosing(WindowEvent arg0) {
			VentanaLogin.setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
			if(JOptionPane.showConfirmDialog(VentanaLogin, "¿Esta seguro que desea salir de la aplicación? \n   Los cambios no guardados se perderán","ATENCION!",JOptionPane.OK_CANCEL_OPTION) == JOptionPane.YES_OPTION){
				System.exit(0);
			}
		}
	}
}
